package com.vhall.component.service.interactive.utils;

import cn.hutool.core.io.FileUtil;
import com.itextpdf.text.pdf.PdfReader;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public class Reader {

    /**
     * description: 静态方法，用于判断文件类型，并返回页数
     *
     * @param file 上传文件
     */
    public static int getFilePageNum(MultipartFile file) throws IOException {
        int pageNum = 0;
        String name = file.getOriginalFilename();
        String ext = FileUtil.extName(name);
        switch (ext) {
            case "xls":
                HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
                Integer sheetNums = workbook.getNumberOfSheets();
                if (sheetNums > 0) {
                    pageNum = workbook.getSheetAt(0).getRowBreaks().length + 1;
                }
                break;
            case "xlsx":
                XSSFWorkbook xwb = new XSSFWorkbook(file.getInputStream());
                Integer sheetNumsx = xwb.getNumberOfSheets();
                if (sheetNumsx > 0) {
                    pageNum = xwb.getSheetAt(0).getRowBreaks().length + 1;
                }
                break;
            //此方法不准确,经常统计出页数为1,慎用
//            case "docx":
//                XWPFDocument docx = new XWPFDocument(file.getInputStream());
//                pageNum = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
//                break;
//            case "doc":
//                HWPFDocument wordDoc = new HWPFDocument(file.getInputStream());
//                pageNum = wordDoc.getSummaryInformation().getPageCount();
//                pageNum = getDocPageNum(file);
//                break;
            case "ppt":
                //ppt操作类
                HSLFSlideShow hslfSlideShow = new HSLFSlideShow(file.getInputStream());
                //获取幻灯片数量(相当于页数)
                pageNum = hslfSlideShow.getSlides().size();
                break;
            case "pptx":
                //pptx操作类
                XMLSlideShow xslideShow = new XMLSlideShow(file.getInputStream());
                //获取幻灯片数量(相当于页数)
                pageNum = xslideShow.getSlides().size();
                break;
            case "pdf":
                //pdf操作类
                PdfReader reader = new PdfReader(file.getInputStream());
                //获取页数
                pageNum = reader.getNumberOfPages();
                break;
            default:
                break;
        }
        return pageNum;
    }


    /**
     * description: 静态方法，专门用于判断Office 2003版本之前的Word（格式为.doc）的页数
     *
     * @param file 上传文件
     */
    //此方法也有问题,需要配合下载word转pdf软件且只能在windows上使用(暂不使用)
    private static int getDocPageNum(MultipartFile file) {
        int pageNum = 0;
        try {
            // 建立ActiveX部件
            ActiveXComponent wordCom = new ActiveXComponent("Word.Application");
            //word应用程序不可见
            wordCom.setProperty("Visible", false);
            // 返回wrdCom.Documents的Dispatch
            //Documents表示word的所有文档窗口（word是多文档应用程序）
            Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();

            // 调用wrdCom.Documents.Open方法打开指定的word文档，返回wordDoc
            Dispatch wordDoc = Dispatch.call(wrdDocs, "Open", file.getInputStream(), false, true, false).toDispatch();
            Dispatch selection = Dispatch.get(wordCom, "Selection").toDispatch();
            //总页数
            // 显示修订内容的最终状态
            pageNum = Integer.parseInt(Dispatch.call(selection, "information", 4).toString());

            //关闭文档且不保存
            Dispatch.call(wordDoc, "Close", new Variant(false));
            //退出进程对象
            wordCom.invoke("Quit", new Variant[]{});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageNum;
    }
}
